import path from 'path'
import fs from 'fs/promises'
import { glob } from 'glob'

async function checkFile() {
    const src = path.join(import.meta.dirname, '../../data/json/*.*').replace(/\\/g, '/')
    const files = await glob(src)

    const failIndex = {}

    for (let i = 0; i < files.length; i++) {
        const filePath = files[i];
        const index = Number(filePath.split("\\").pop().split('.')[0].split('-')[0])
        failIndex[index] = true;
    }

    for (let i = 1; i < 4369; i++) {
        if (!failIndex[i]) console.log('缺少的文件' + i)
    }
}

async function errorFile() {
    const src = path.join(import.meta.dirname, '../../data/json/**/*.json').replace(/\\/g, '/')
    const files = await glob(src)


    for (let i = 0; i < files.length; i++) {
        const filePath = files[i];
        const file = await fs.readFile(filePath, 'utf-8')
        try {
            const json = JSON.parse(file)
            if (json.answer.indexOf('失败') != -1) throw '分析失败'
        } catch (e) {
            console.error(filePath, e);
            const newFilePath = filePath.replace('.json', '-error.txt')
            await fs.rename(filePath, newFilePath)

        }
    }
}

checkFile();
// errorFile();